package com.lobo.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Tag;
import com.lobo.blog.exception.AllReadyBeenDeletedException;
import com.lobo.blog.exception.TagNotFoundException;
import com.lobo.blog.mappers.TagsMapper;
import com.lobo.blog.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/9/29
 * @description:
 */
@Service
public class TagServiceImpl implements TagService {

    @Resource
    TagsMapper TagsMapper;

    @Override
    public int saveTag(Tag Tag) {
        if (Tag.getName() == null || "".equals(Tag.getName())) {
            System.out.println("名称为空");
            return 0;
        }
        Tag exists = TagsMapper.findTagByName(Tag.getName());
        if (exists != null) {
            Date now = new Date();
            exists.setUpdateTime(now);
            TagsMapper.updateTagDeletedStatusToFalse(exists);
            return 1;
        } else {
            Date now = new Date();
            Tag.setCreateTime(now);
            Tag.setUpdateTime(now);
            return TagsMapper.addNewTag(Tag);
        }
    }

    @Override
    public Tag getTagByTagId(Integer TagId) throws TagNotFoundException {
        Tag Tag = TagsMapper.getTagById(TagId);
        if (Tag != null) {
            return Tag;
        } else {
            throw new TagNotFoundException();
        }
    }

    @Override
    public PageInfo<Tag> getTags(Integer pageNum) {
        //开启分页功能（类似于拦截器，拦截下一条查询语句，并加上limit语句）
        PageHelper.startPage(pageNum, 10);
        List<Tag> list = TagsMapper.getAllTagByList();
        PageInfo page = new PageInfo(list);
        System.out.println(page);
        return page;
    }

    @Override
    public List<Tag> getAllTags() {
        return TagsMapper.getAllTagByList();
    }

    @Override
    public int updateTag(Tag Tag) {
        Tag exists = TagsMapper.findTagByName(Tag.getName());
        if (exists == null) {
            Date now = new Date();
            Tag.setUpdateTime(now);
            return TagsMapper.updateTag(Tag);
        } else {
            int deleteStatus = exists.getIsDeleted();
            //是否被删除，非0为真
            if (deleteStatus == 1) {
                Date now = new Date();
                Tag.setUpdateTime(now);
                exists.setUpdateTime(now);
                TagsMapper.updateTagDeletedStatusToFalse(exists);
                return TagsMapper.updateTagDeletedStatusToTrue(Tag);
            } else {
                return 0;
            }
        }
    }

    @Override
    public int deleteTagById(Integer id) throws AllReadyBeenDeletedException {
        int result = TagsMapper.deleteTagById(id);
        if (result == 0) {
            throw new AllReadyBeenDeletedException();
        }
        return 1;
    }

    @Override
    public List<Tag> getTagByIds(String ids) {
        List<Tag> tagList = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            tagList = TagsMapper.getTagByIds(ids);
        }
        return tagList;
    }

    /**
     * 获取最近修改过的几个tag
     *
     * @param size
     * @return
     */
    @Override
    public List<Tag> getTopTagBySize(Integer size) {
        return TagsMapper.getTopSizeTag(size);
    }
}
